package com.hejing.String;

import org.junit.Test;

/**
 * ClassName: StringDemo01
 * Package: com.hejing.String
 * Description:
 *
 * @Author: HeJing
 * @Create: 2023-04-16 15:13
 * @Version: 1.0
 */
public class StringDemo01 {
    @Test
    public void test01(){
        String s1 = "hello";
        String s2 = "hello";

        String s3 = new String("hello");
        String s4 = new String("hello");

        System.out.println(s1 == s2);   // true
        System.out.println(s1 == s3);   // false
        System.out.println(s3 == s4);   // false
        System.out.println(s1.equals(s4));  // true
    }
    
    @Test
    public void test02(){
        Person p1  = new Person();
        Person p2 = new Person();
        p1.name = "Tom";
        p2.name = "Tom";
        p1.name = "Jerry";

        System.out.println(p1.name);
        System.out.println(p2.name);
    }

    /**
     * 测试字符串中的连接符: + :
     *
     */
    @Test
    public void test03(){
        String s1 = "hello";
        String s2 = "world";

        String s3 = "helloworld";
        String s4 = "hello" + "world";

        // 通过查看了字节码文件发现当出现变量参与+连接时，会调用StringBuilder的toString()方法--> new String()
        // 因此，下面的 s5, s6, s7 都指向了new出来的三个不同的地址空间...
        String s5 = s1 + "world";
        String s6 = "hello" + s2;
        String s7 = s1 + s2;

        System.out.println(s3 == s4);   // true
        System.out.println(s3 == s5);   // true
        System.out.println(s3 == s6);   // true
        System.out.println(s3 == s7);   // true
        System.out.println(s5 == s6);   // true
        System.out.println(s5 == s7);   // true

        System.out.println("*********************** intern test ******************************");
        String s8 = s5.intern();    // 返回的是字符串常量池中字面量的地址。
        System.out.println(s8);         // helloworld
        System.out.println(s3 == s8);   // true
        System.out.println(s4 == s8);   // true
        System.out.println(s5 == s8);   // false
    }

    /**
     * concat()测试
     */
    @Test
    public void test04(){
        String s1 = "hello";
        String s2 = "world";

        final String ss1 = "你好";
        final String ss2 = "Java";

        String s3 = s1.concat(s2);
        String s4 = "hello".concat("world");
        System.out.println(s3 == s4);  // false

        String ss3 = ss1.concat(ss2);
        String ss4 = "你好".concat("Java");
        System.out.println(ss3 == ss4);  // 同样是 false
    }
}

class Person {
    String name;
    
}

























